package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.enumeration.OperationType;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;


    /**
     * 新增套餐
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public Result addSetMeal(SetmealDTO dto) {
        Setmeal s = setmealMapper.getByName(dto.getName());
        if (s != null){
            return Result.error(MessageConstant.NAME_EXIST);
        }
        //新建一个对象，将前端传回来的东西复制过去
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto,setmeal);
        //调用mapper方法添加到表格中
        setmealMapper.addSetMeal(setmeal);
        //获取到生成到套餐的id
        Long setmealId = setmeal.getId();
        //获取到套餐菜品关系的集合
        List<SetmealDish> dishes = dto.getSetmealDishes();
        dishes.forEach(setmealDish -> {
            //遍历集合，为每一个套餐id赋值
            setmealDish.setSetmealId(setmealId);
            log.info("setmealDish:{}",setmealDish);
        });
        setMealDishMapper.addSetMealDish(dishes);

        return Result.success();
    }

    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public Result pageQuery(SetmealPageQueryDTO dto) {
        //判断非空
        if (dto.getPage() == null){
            dto.setPage(1);
        }
        if (dto.getPageSize() == null){
            dto.setPageSize(5);
        }
        //调用工具开始分页
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        //调用mapper方法开启条件查询
        Page<Setmeal> page = setmealMapper.pageQuery(dto);
        //获取到总记录数和当前页所有数据
        long total = page.getTotal();
        List<Setmeal> result = page.getResult();
        //新建一个vo集合用来存储返回到前端页面的每条数据
        List<SetmealVO> list = new ArrayList<>();
        //便利当前页所有数据，设置id和名字
        result.forEach(setmeal -> {
            //新建一个对象用于返回给前端
            SetmealVO setmealVO = new SetmealVO();
            //将前端返回到的内容复制到新的对象中
            BeanUtils.copyProperties(setmeal,setmealVO);
            //根据id获取名字，然后得到套餐名字
            Category category = categoryMapper.selectById(setmealVO.getCategoryId());
            //设置套餐名字
            setmealVO.setCategoryName(category.getName());
            //将内容添加到vo的集合
            list.add(setmealVO);
        });
        PageResult pageResult = new PageResult();
        pageResult.setTotal(total);
        pageResult.setRecords(list);
        return Result.success(pageResult);
    }

    /**
     * 根据id删除套餐
     *
     * @param ids
     * @return
     */
    @Override
    public Result deleteSetMeal(List<Long> ids) {
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.selectById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                return Result.error(MessageConstant.DISH_ON_SALE);
            }
        }
            ids.forEach(id -> {
                setmealMapper.deleteById(id);
                setMealDishMapper.deleteBySetMealId(id);
            });
        log.info("删除成功");
            return Result.success();
        }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public Result getById(Long id) {
        if (id == null){
            return Result.error(MessageConstant.ID_NOT_EXIST);
        }
        List<SetmealDish> bySetMealId = setMealDishMapper.getBySetMealId(id);
        Setmeal setmeal = setmealMapper.getById(id) ;
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(bySetMealId);
        return Result.success(setmealVO);
    }

    /**
     * 修改套餐信息
     *
     * @param dto
     * @return
     */
    @Override
    public Result updateSetMeal(SetmealDTO dto) {
        if (dto.getId() == null){
            return Result.error(MessageConstant.ID_NOT_EXIST);
        }
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto,setmeal);
        setmealMapper.updateSetMeal(setmeal);
        //直接删除所有菜品
        setMealDishMapper.deleteById(Arrays.asList(setmeal.getId()));

        List<SetmealDish> list = dto.getSetmealDishes();
        log.info("list:{}",list);
        if (list != null && list.size() > 0){
            list.forEach(setmealDish -> setmealDish.setSetmealId(setmeal.getId()));
        }else {
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
            setMealDishMapper.addSetMealDish(list);
        return Result.success();
    }

    /**
     * 修改售卖状态
     * @param status
     * @param id
     * @return
     */
    @Override
    public Result updateStatus(Integer status, Long id) {
        //先判断是否包含了包含停售的菜品
        List<Dish> list = dishMapper.getBySetMealId(id);
        if (list != null && list.size() > 0) {
            for (Dish dish : list) {
                if (StatusConstant.DISABLE == dish.getStatus()){
                    return Result.error(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            }
        }
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        setmealMapper.updateSetMeal(setmeal);
        return Result.success();
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }
}
